home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 18.8 KB | 600 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // PascalString.cp
- // Copyright © 1985-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __PASCALSTRING__
- #include "PascalString.h"
- #endif
-
- // MacApp
-
- // Toolbox
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- // ANSI
-
- #ifndef __LIMITS__
- #include <Limits.h>
- #endif
-
- #if qDebugMsg
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
- #endif
-
- #pragma segment Main
-
- //----------------------------------------------------------------------------------------
- // static data members
- //----------------------------------------------------------------------------------------
- Handle CPascalStr::fgItl2Handle;
-
- //========================================================================================
- // GLOBAL Functions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // MABlockMove:
- //----------------------------------------------------------------------------------------
-
- #if !qPowerPC && (UINT_MAX < ULONG_MAX)
- void MABlockMove(const void* srcPtr, void* destPtr, long byteCount)
- {
- // We think size_t is at least a long, so we don't need a cast.
- // If we get a compiler warning, we're in trouble.
- if (byteCount < UINT_MAX) // size_t could be an unsigned 16 bit value
- ::memcpy(destPtr, srcPtr, byteCount);
- else
- BlockMoveData(srcPtr, destPtr, byteCount);
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // MABlockMoveOverlap:
- //----------------------------------------------------------------------------------------
-
- #if !qPowerPC && (UINT_MAX < ULONG_MAX)
- void MABlockMoveOverlap(const void* srcPtr, void* destPtr, long byteCount)
- {
- // We think size_t is at least a long, so we don't need a cast.
- // If we get a compiler warning, we're in trouble.
- if (byteCount < UINT_MAX) // size_t could be an unsigned 16 bit value
- ::memmove(destPtr, srcPtr, byteCount);
- else
- BlockMoveData(srcPtr, destPtr, byteCount);
- }
- #endif
-
- //========================================================================================
- // CLASS CCharStr
- //========================================================================================
- //----------------------------------------------------------------------------------------
- // CCharStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CCharStr::CopyFrom(const unsigned char* str, unsigned char maxLength)
- {
- size_t possibleLength = 0;
- if (str)
- {
- possibleLength = str[0];
- possibleLength = possibleLength < maxLength ? possibleLength : maxLength;
- }
-
- MABlockMove(&str[1], this, possibleLength);
- ((char*)this)[possibleLength] = 0;
- }
-
- //----------------------------------------------------------------------------------------
- // CCharStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CCharStr::CopyFrom(const char* str, size_t maxLength)
- {
- size_t possibleLength = 0;
- if (str)
- {
- possibleLength = strlen(str);
- possibleLength = possibleLength < maxLength ? possibleLength : maxLength;
- }
-
- MABlockMove(str, this, possibleLength);
- ((char*)this)[possibleLength] = 0;
- }
-
- //----------------------------------------------------------------------------------------
- // CCharStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CCharStr::CopyFrom(const void* str, size_t forLength)
- {
- //
- // NOTE: there is _NO_ maxlength protection here, be careful!
- // However, since Length() is an unsigned char
- // its maximum value is kStr255Len anyways.
- //
- MABlockMove(str, this, forLength);
- ((char*)this)[forLength] = 0;
- }
-
- //========================================================================================
- // CLASS CPascalStr
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyFrom(const unsigned char* str, unsigned char maxLength)
- {
- unsigned char possibleLength = 0;
- if (str)
- {
- possibleLength = str[0];
- possibleLength = possibleLength < maxLength ? possibleLength : maxLength;
- }
-
- Length() = possibleLength;
- MABlockMove(&str[1], &(*this)[1], Length());
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyFrom(const char* str, unsigned char maxLength)
- {
- unsigned char possibleLength = 0;
- if (str)
- {
- possibleLength = (unsigned char)strlen(str);
- possibleLength = possibleLength < maxLength ? possibleLength : maxLength;
- }
-
- Length() = possibleLength;
- MABlockMove(str, &(*this)[1], Length());
-
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyFrom(const void* str, unsigned char forLength)
- {
- //
- // NOTE: there is _NO_ maxlength protection here, be careful!
- // However, since Length() is an unsigned char
- // its maximum value is kStr255Len anyways.
- //
- Length() = forLength;
- MABlockMove(str, &(*this)[1], Length());
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyFrom(const unsigned char* str)
- {
- //
- // NOTE: there is _NO_ maxlength protection here, be careful!
- // However, since Length() is an unsigned char
- // its maximum value is kStr255Len anyways.
- //
- Length() = str[0];
- MABlockMove(&str[1], &(*this)[1], Length());
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyFrom(const char* str)
- {
- //
- // NOTE: there is _NO_ maxlength protection here, be careful!
- // However, since Length() is an unsigned char
- // its maximum value is kStr255Len anyways.
- //
- Length() = strlen(str);
- MABlockMove(str, &(*this)[1], Length());
- }
-
- #if !(qPowerPC || qNeedsMC68020)
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyFrom(unsigned long id)
- {
- //
- // NOTE: there is _NO_ maxlength protection here, be careful!
- // However, since Length() is an unsigned char
- // its maximum value is kStr255Len anyways.
- //
- Length() = sizeof(id);
- MABlockMove(&id, &(*this)[1], Length());
- }
- #endif
-
-
- #if !(qPowerPC || qNeedsMC68020)
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyFrom
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyFrom(unsigned short num)
- {
- //
- // NOTE: there is _NO_ maxlength protection here, be careful!
- // However, since Length() is an unsigned char
- // its maximum value is kStr255Len anyways.
- //
- Length() = sizeof(num);
- MABlockMove(&num, &(*this)[1], Length());
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyTo
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyTo(unsigned char* str) const
- {
- MABlockMove(&(*this)[0], str, Length() + 1);
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyTo
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyTo(char* str) const
- {
- MABlockMove(&(*this)[1], str, Length());
- str[Length()] = 0; // Add null terminations
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::CopyTo
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::CopyTo(void* str) const
- {
- MABlockMove(&(*this)[1], str, Length());
- }
- //----------------------------------------------------------------------------------------
- // CPascalStr::GetMaxLength:
- //----------------------------------------------------------------------------------------
-
- unsigned char CPascalStr::GetMaxLength(unsigned char pos, unsigned char length) const
- {
- short testLength = Length() - pos + kLengthByte;
-
- testLength = (length > testLength) ? testLength : length;
-
- if (testLength < 0)
- testLength = 0;
-
- return (unsigned char)testLength;
- }
-
- #if !(qPowerPC || qNeedsMC68020)
- //----------------------------------------------------------------------------------------
- // CPascalStr::ToULong
- //----------------------------------------------------------------------------------------
-
- unsigned long CPascalStr::ToULong() const
- {
- //
- // NOTE: there is _NO_ maxlength protection here, be careful!
- // However, since Length() is an unsigned char
- // its maximum value is kStr255Len anyways.
- //
- unsigned long returnVal;
- MABlockMove(&(*this)[1], &returnVal, sizeof(returnVal));
-
- return returnVal;
- }
- #endif
-
- #if !(qPowerPC || qNeedsMC68020)
- //----------------------------------------------------------------------------------------
- // CPascalStr::ToULong
- //----------------------------------------------------------------------------------------
-
- unsigned short CPascalStr::ToUShort() const
- {
- //
- // NOTE: there is _NO_ maxlength protection here, be careful!
- // However, since Length() is an unsigned char
- // its maximum value is kStr255Len anyways.
- //
- unsigned short returnVal;
- MABlockMove(&(*this)[1], &returnVal, sizeof(returnVal));
-
- return returnVal;
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // Definitions for relational string operators helpers
- //----------------------------------------------------------------------------------------
-
- short CPascalStr::CPascalStrCompare(const CPascalStr& s1, const CPascalStr& s2)
- {
- return ::CompareText(&s1[1], &s2[1], s1.Length(), s2.Length(), CPascalStr::fgItl2Handle);
- }
-
- short CPascalStr::CPascalStrCompare(const CPascalStr& s1, const char* s2)
- {
- return ::CompareText(&s1[1], s2, s1.Length(), s2 ? strlen(s2) : 0, CPascalStr::fgItl2Handle);
- }
-
- short CPascalStr::CPascalStrCompare(const CPascalStr& s1, unsigned char ch)
- {
- return ::CompareText(&s1[1], &ch, s1.Length(), 1, CPascalStr::fgItl2Handle);
- }
-
- short CPascalStr::CPascalStrCompare(const char* s1, const CPascalStr& s2)
- {
- return ::CompareText(s1, &s2[1], s1 ? strlen(s1) : 0, s2.Length(), CPascalStr::fgItl2Handle);
- }
-
- short CPascalStr::CPascalStrCompare(unsigned char ch, const CPascalStr& s2)
- {
- return ::CompareText(&ch, &s2[1], 1, s2.Length(), CPascalStr::fgItl2Handle);
- }
-
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::operator+:
- //----------------------------------------------------------------------------------------
-
- CStr255 operator+(const CPascalStr& s1, const CPascalStr& s2)
- {
- CStr255 newStr;
-
- if (s1.Length() + s2.Length() > kStr255Len)
- newStr.Length() = kStr255Len;
- else
- newStr.Length() = s1.Length() + s2.Length();
-
- MABlockMove(&s1[1], &newStr[1], s1.Length());
- MABlockMove(&s2[1], &newStr[s1.Length() + kLengthByte], newStr.Length() - s1.Length());
-
- return newStr;
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::operator+:
- //----------------------------------------------------------------------------------------
-
- CStr255 operator+(const CPascalStr& s1, const char* s2)
- {
- CStr255 newStr;
- short s2Len = (s2) ? (short)(strlen(s2)) : 0;
-
- if (s1.Length() + s2Len > kStr255Len)
- newStr.Length() = kStr255Len;
- else
- newStr.Length() = s1.Length() + s2Len;
-
- MABlockMove(&s1[1], &newStr[1], s1.Length());
- MABlockMove(s2, &newStr[s1.Length() + kLengthByte], newStr.Length() - s1.Length());
-
- return newStr;
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::operator+:
- //----------------------------------------------------------------------------------------
-
- CStr255 operator+(const CPascalStr& s1, unsigned char ch)
- {
- CStr255 newStr;
- short s2Len = 1;
-
- if (s1.Length() + s2Len > kStr255Len)
- newStr.Length() = kStr255Len;
- else
- newStr.Length() = s1.Length() + s2Len;
-
- MABlockMove(&s1[1], &newStr[1], s1.Length());
- MABlockMove(&ch, &newStr[s1.Length() + kLengthByte], newStr.Length() - s1.Length());
-
- return newStr;
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::operator+:
- //----------------------------------------------------------------------------------------
-
- CStr255 operator+(const char* s1, const CPascalStr& s2)
- {
- CStr255 newStr;
- short s1Len = (s1) ? (short)(strlen(s1)) : 0;
-
- if (s1Len + s2.Length() > kStr255Len)
- newStr.Length() = kStr255Len;
- else
- newStr.Length() = s1Len + s2.Length();
-
- MABlockMove(s1, &newStr[1], s1Len);
- MABlockMove(&s2[1], &newStr[s1Len + kLengthByte], newStr.Length() - s1Len);
-
- return newStr;
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::operator+:
- //----------------------------------------------------------------------------------------
-
- CStr255 operator+(unsigned char ch, const CPascalStr& s2)
- {
- CStr255 newStr;
- short s1Len = 1;
-
- if (s1Len + s2.Length() > kStr255Len)
- newStr.Length() = kStr255Len;
- else
- newStr.Length() = s1Len + s2.Length();
-
- MABlockMove(&ch, &newStr[1], s1Len);
- MABlockMove(&s2[1], &newStr[s1Len + kLengthByte], newStr.Length() - s1Len);
-
- return newStr;
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::Insert
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::Insert(const char* insStr, unsigned char insStrLength, unsigned char pos, unsigned char maxLength)
- {
- if (insStr)
- {
- short shortPos = pos;
- if (shortPos > Length() + 1)
- {
- #if qDebugMsg
- fprintf(stderr, "###CPascalStr::Insert: Insert position greater than length of CPascalStr.\n");
- #endif
- if (Length() < maxLength)
- shortPos = Length() + 1;
- }
-
- #if qDebugMsg
- if ((short)Length() + insStrLength > maxLength)
- fprintf(stderr, "### CPascalStr::Insert: CPascalStr truncated during insert call.\n");
- #endif
-
- short usableLengthOfInsertString;
- short endPosOfInsertString;
- short usableLengthOfShiftedString;
-
- if (shortPos + insStrLength > maxLength)
- usableLengthOfInsertString = maxLength - shortPos + 1;
- else
- usableLengthOfInsertString = insStrLength;
- endPosOfInsertString = shortPos + usableLengthOfInsertString - 1;
-
- if ((endPosOfInsertString + 1) + (Length() - shortPos + 1) > maxLength)
- usableLengthOfShiftedString = maxLength - endPosOfInsertString;
- else
- usableLengthOfShiftedString = Length() - shortPos + 1;
-
- MABlockMoveOverlap(&(*this)[shortPos], &(*this)[endPosOfInsertString + 1], usableLengthOfShiftedString);
- MABlockMove(insStr, &(*this)[shortPos], usableLengthOfInsertString);
- Length() = usableLengthOfShiftedString + endPosOfInsertString;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::Insert
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::Insert(const char* insStr, unsigned char pos, unsigned char maxLength)
- {
- if (insStr)
- Insert(insStr, strlen(insStr), pos, maxLength);
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::Insert
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::Insert(const char* insStr, unsigned char pos)
- {
- if (insStr)
- Insert(insStr, strlen(insStr), pos, kStr255Len);
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::Insert
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::Insert(const CPascalStr& insStr, unsigned char pos, unsigned char maxLength)
- {
- Insert((const char*)&insStr[1], insStr.Length(), pos, maxLength);
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::Insert
- //----------------------------------------------------------------------------------------
-
- void CPascalStr::Insert(const CPascalStr& insStr, unsigned char pos)
- {
- Insert((const char*)&insStr[1], insStr.Length(), pos, kStr255Len);
- }
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::Pos(char*):
- //----------------------------------------------------------------------------------------
-
- unsigned char CPascalStr::Pos(const char* subStr, unsigned char startPos)
- {
- char cStr[kStr255Len + 1];
- char* ptr;
- MABlockMove(&(*this)[1], cStr, Length());
- cStr[Length()] = 0;
- ptr = strstr((const char*)&cStr[startPos - 1], subStr);
- return (ptr) ? (ptr - (const char*)cStr) + 1 : 0;
-
- } // CPascalStr::Pos(char*)
-
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::Pos(CPascalStr):
- //----------------------------------------------------------------------------------------
-
- unsigned char CPascalStr::Pos(const CPascalStr& subStr, unsigned char startPos)
- {
- char cStr[kStr255Len + 1];
-
- MABlockMove(&subStr[1], cStr, subStr.Length());
- cStr[subStr.Length()] = 0;
- return Pos(cStr, startPos);
-
- } // CPascalStr::Pos(CPascalStr)
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::Delete
- //----------------------------------------------------------------------------------------
- void CPascalStr::Delete(unsigned char pos, unsigned char length)
- {
- if ((pos > 0) && (length > 0) && (pos <= Length())) // should also check that pos <= kMaxLength
- {
- if (pos + length > Length())
- Length() = pos - 1;
- else
- {
- MABlockMoveOverlap(&(*this)[pos + length], &(*this)[pos], Length() - (pos + length) + kLengthByte);
- Length() -= length;
- }
- }
- } // CPascalStr::Delete
-
-
- //----------------------------------------------------------------------------------------
- // CPascalStr::SetItl2Handle
- //----------------------------------------------------------------------------------------
-
- Handle CPascalStr::SetItl2Handle(Handle itsItl2Handle)
- {
- Handle returnVal = fgItl2Handle;
- fgItl2Handle = itsItl2Handle;
-
- return returnVal;
- }
-
- //----------------------------------------------------------------------------------------
- // End of PascalString.cp
-
- #pragma segment Inline
-